<?php

class FSiteController extends FController
{
	public function actionIndex()
	{
		$this->render('index');
	}
    public function actionError(){
        $this->layout='error';
        if($error=Yii::app()->errorHandler->error)
        {
            if(Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }
    public function actionLogin(){
        //$enc = NEW bCrypt();
        // echo $enc->hash(123456);
        //die();
        $this->pageTitle = t('FCMS BACKEND ZONE - PLEASE LOGIN','fcms-pageTitle');
        $model=new UserLoginForm;

        // if it is ajax validation request
        $this->performAjaxValidation($model,'login-form');

        // collect user input data
        if(isset($_POST['UserLoginForm']))
        {
            $model->attributes=$_POST['UserLoginForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate() && $model->login())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        $this->layout = 'login';
        $this->render('login',array('model'=>$model));
    }
    public function actionLogout($token) {
        if ($token !== Yii::app()->getRequest()->getCsrfToken())
            throw new CHttpException(400, t('Invalid request. Please do not repeat this request again.'));

        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }
    protected function performAjaxValidation($model,$form)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']===$form)
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }


	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}